﻿{% extends 'admin/base.html' %}

{%load comm_extras %}

{% block head_soruce %}
<script type="text/javascript" src="/static/common/js/jquery.simplemodal.1.4.min.js"></script> 
<script type="text/javascript" src="/static/admin/js/admin_master.js"></script> 
<script type="text/javascript">
	function checkData(){
		var title = jQuery.trim($("#title").val());
		if(title == ""){
			alert("名称不能为空");
			return false;
		}
		var re = /^[1-9]+[0-9]*]*$/;
		var index = jQuery.trim($("#index").val());
		if(index == "" || !re.test(index)){
			alert("请输入正确的序号");
			return false;
		}
		var url = "{%url admin-category-add-json%}";
        var	data = {'title':title, 'index':index, 'parent_id':0, 'level':1};
		$.ajax({
			url: url,
			type: 'post',
			dataType: 'json',
			data:data,
			success: function(result, textStatus){
				if(result){
					if(result.success){
						window.location.href = "{%url admin-category-index %}";
					}else{
						alert(result.err_msg);
					}
				}else{
					alert('添加失败');
				}
			},
			error: function(xhr, textStatus, errorThrown){
			}
		});
	}

	function checkSecData(){
		var title = jQuery.trim($("#sec_title").val());
		if(title == ""){
			alert("名称不能为空");
			return false;
		}
		var re = /^[1-9]+[0-9]*]*$/;
		var index = jQuery.trim($("#sec_index").val());
		if(index == "" || !re.test(index)){
			alert("请输入正确的序号");
			return false;
		}
		var url = "{%url admin-category-add-json%}";

        var	data = {'title':title, 'index':index, 'parent_id':$("#selParentId").val(), 'level':2};
		$.ajax({
			url: url,
			type: 'post',
			dataType: 'json',
			data:data,
			success: function(result, textStatus){
				if(result){
					if(result.success){
						window.location.href = "{%url admin-category-index %}";
					}else{
						alert(result.err_msg);
					}
				}else{
					alert('添加失败');
				}
			},
			error: function(xhr, textStatus, errorThrown){
			}
		});
	}

</script>
{% endblock %}

{% block main_content %}

	<ul id="movie-form" class="form">
	    <li>
            <h1>第一级</h1>
        </li>
		<li>
            <label for="title">名称:</label>
            <input type="text" id="title" name="title" value="" class="inputText" />
        </li>
		<li>
            <label for="index">序号:</label>
            <input type="text" id="index" name="index" value="" class="inputText" />
        </li>
		<li>
			<br/>
			<input type="submit" id="btnAdd" value="添加第一级分类" onclick="checkData();" style="margin-left:500px"/>
		</li>
	</ul>

	<ul id="movie-form" class="form">
	    <li>
            <h1>第二级</h1>
        </li>
		<li>
            <label for="parent">父分类:</label>
            <select id="selParentId">
				{%if hasTopCategory%}
					{%for c in categorys%}
						<option value ="{{c.id}}">{{c.title}}</option>
					{%endfor%}
				{%else%}
					<option value ="0">无</option>
				{%endif%}
			</select>
        </li>
		<li>
            <label for="sec_title">名称:</label>
            <input type="text" id="sec_title" name="sec_title" value="" class="inputText" />
        </li>
		<li>
            <label for="sec_index">序号:</label>
            <input type="text" id="sec_index" name="sec_index" value="" class="inputText" />
        </li>
		<li>
			<br/>
			<input type="submit" id="btnAddSec" value="添加第二级分类" {%if hasTopCategory%}{%else%}disabled="disabled"{%endif%} onclick="checkSecData();" style="margin-left:500px"/>
		</li>
	</ul>

{% endblock %}
